package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealAndCheckGroupDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealAndCheckGroupDao setmealAndCheckGroupDao;
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<Setmeal> page = setmealDao.pageQuery(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(List<Integer> checkgroupIds, Setmeal setmeal) {
        log.info("[套餐-新增]data：{}");
        Setmeal setmeal1 = new Setmeal();
        BeanUtils.copyProperties(setmeal, setmeal1);
        //将套餐信息存入数据库
        setmealDao.addSetmeal(setmeal1);
        //将套餐、检查组关系存入关系表中
        for (Integer id : checkgroupIds) {
            setmealAndCheckGroupDao.add(setmeal1.getId(),id);
        }
    }

}


